//package cn.com.bsoft.cloud;
//
//import cn.com.bsoft.cloud.entity.company.PTCompanyMess;
//import cn.com.bsoft.cloud.service.CloudCompanyService;
//import cn.com.bsoft.cloud.utils.AesUtils;
//import groovy.util.logging.Slf4j;
//import org.apache.tomcat.util.codec.binary.Base64;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.WebApplicationContext;
//import org.springframework.web.context.support.WebApplicationContextUtils;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//@Component
//@WebFilter(filterName = "scheduleFilter" ,urlPatterns = "/schedule/*")
//@Slf4j
//public class ScheduleFilter implements Filter {
//
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private CloudCompanyService cloudCompanyService;
//
//    @Override
//    public void init(FilterConfig filterConfig) {
//        ServletContext sc = filterConfig.getServletContext();
//        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
//        if(cxt != null && cxt.getBean(CloudCompanyService.class) != null && cloudCompanyService == null) {
//            cloudCompanyService =cxt.getBean(CloudCompanyService.class);
//        }
//        logger.info("TestFilter init");
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){
//        String resultMessage ="error";
//        try {
//
//            ServletRequest requestWrapper = null;
//            HttpServletRequest request = (HttpServletRequest) servletRequest;
//            String token = request.getHeader("token");
//            String companyCode = request.getHeader("companyCode");
//            PTCompanyMess companyMess = cloudCompanyService.getCompanyMess(companyCode);
//
//            if(servletRequest instanceof HttpServletRequest && token!=null
//                    &&!token.isEmpty() && companyCode!=null &&
//                    !companyCode.isEmpty() && null != companyMess) {
//                WrapperResponse wrapResponse = new WrapperResponse((HttpServletResponse)servletResponse);
//                requestWrapper = new CompanyHttpServletRequestWrapper((HttpServletRequest) servletRequest,companyMess,token);
//                filterChain.doFilter(requestWrapper, wrapResponse);
//                String data = wrapResponse.getContent();
//                byte[] aesData = AesUtils.encrypt(data,companyMess.getSecret());
//                String  aesBase64Str = Base64.encodeBase64String(aesData);
//
//                HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//                httpServletResponse.getOutputStream().write(aesBase64Str.getBytes("utf-8"));
//                httpServletResponse.setContentLength(aesBase64Str.getBytes("utf-8").length);
//                httpServletResponse.flushBuffer();
//
//                byte[] bb = Base64.decodeBase64(aesBase64Str);
//                byte[] desByte = AesUtils.decrypt(bb,companyMess.getSecret());
//                String dec = new String(desByte,"utf-8");
//
//            }else {
//                HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
//                httpServletResponse.getOutputStream().write(resultMessage.getBytes("utf-8"));
//                httpServletResponse.setContentLength(resultMessage.getBytes("utf-8").length);
//                httpServletResponse.flushBuffer();
//            }
//        }catch (Exception e){
//           e.printStackTrace();
//        }
//
//    }
//
//    @Override
//    public void destroy() {
//
//    }
//}
